package managers.dateManager.beginFrozen.durationChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;
import org.tbjd.client.states.StateBeginFrozenSPEC;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * A task with multiple predecessors and successors as well as isolated tasks
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * Duration of the task "task" is changed
 * </p>
 * <b>Expected</b>
 * <ul>
 * <li>update EndDate of "task"</li>
 * <li>update successors in ASAP ChainingMode of "task"</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class BeginFrozen_DurationChange_PredecessorsAndSuccessors_Medium extends TestCase {

	/*
	 * Task		Begins 		Ends 		Predecessor Successor 	ChainingMode	task +2 days
	 * o p4		1.12.2008 	4.12.2008 	- 			p2 			ASAP 			3.12.2008 - 6.12.2008
	 * o p3		1.1.2009 	2.1.2009 	- 			p2 			ASAP 			3.1.2009 - 4.1.2009
	 * o p1		1.1.2009 	2.1.2009 	-			task		manual 			-
	 * o t1		1.5.2009 	3.5.2009 	- 			- 			ASAP 			-
	 * o p2 	2.1.2009 	3.1.2009 	-			task		ASAP 			4.1.2009 - 5.1.2009
	 * o task 	4.1.2009 	10.1.2009 	-	 		s1, s2	 	ASAP 			6.1.2009 - 12.1.2009
	 * o t2 	1.1.2009 	3.1.2009 	- 			- 			manual 			-
	 * o s1 	11.1.2009 	14.1.2009 	-			- 			ASAP 			13.1.2009 - 16.1.2009
	 * o s2 	15.1.2009 	23.1.2009 	-			s3 			manual 			-
	 * o t3 	5.6.2009 	12.6.2009 	- 			- 			ASAP 			-
	 * o s3 	8.6.2009 	12.6.2009 	- 			- 			ASAP 			8.6.2009 - 12.6.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		Task p4 = TaskFactory.getInstance();
		p4.setName("p4");
		p4.setBeginDate(df.parse("1.12.2008"));
		p4.setEndDate(df.parse("4.12.2008"));
		p4.setDuration(72);

		Task p3 = TaskFactory.getInstance();
		p3.setName("p3");
		p3.setBeginDate(df.parse("1.1.2009"));
		p3.setEndDate(df.parse("2.1.2009"));
		p3.setDuration(24);

		Task p1 = TaskFactory.getInstance();
		p1.setName("p1");
		p1.setBeginDate(df.parse("1.1.2009"));
		p1.setEndDate(df.parse("2.1.2009"));
		p1.setDuration(24);
		p1.setState(new StateBeginFrozenSPEC(p1.getState().getDurationModeState()));

		Task t1 = TaskFactory.getInstance();
		t1.setName("t1");
		t1.setBeginDate(df.parse("1.5.2009"));
		t1.setEndDate(df.parse("3.5.2009"));
		t1.setDuration(48);

		Task p2 = TaskFactory.getInstance();
		p2.setName("p2");
		p2.setBeginDate(df.parse("2.1.2009"));
		p2.setEndDate(df.parse("3.1.2009"));
		p2.setDuration(24);

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("4.1.2009"));
		this.task.setEndDate(df.parse("10.1.2009"));
		this.task.setDuration(144);

		Task t2 = TaskFactory.getInstance();
		t2.setName("t2");
		t2.setBeginDate(df.parse("1.1.2009"));
		t2.setEndDate(df.parse("3.1.2009"));
		t2.setDuration(48);
		t2.setState(new StateBeginFrozenSPEC(t2.getState().getDurationModeState()));

		Task s1 = TaskFactory.getInstance();
		s1.setName("s1");
		s1.setBeginDate(df.parse("11.1.2009"));
		s1.setEndDate(df.parse("14.1.2009"));
		s1.setDuration(72);

		Task s2 = TaskFactory.getInstance();
		s2.setName("s2");
		s2.setBeginDate(df.parse("15.1.2009"));
		s2.setEndDate(df.parse("23.1.2009"));
		s2.setDuration(192);
		s2.setState(new StateBeginFrozenSPEC(s2.getState().getDurationModeState()));

		Task t3 = TaskFactory.getInstance();
		t3.setName("t3");
		t3.setBeginDate(df.parse("5.6.2009"));
		t3.setEndDate(df.parse("12.6.2009"));
		t3.setDuration(168);

		Task s3 = TaskFactory.getInstance();
		s3.setName("s3");
		s3.setBeginDate(df.parse("8.6.2009"));
		s3.setEndDate(df.parse("12.6.2009"));
		s3.setDuration(168);

		p4.addSuccessor(p2);
		p3.addSuccessor(p2);
		p1.addSuccessor(this.task);
		p2.addSuccessor(this.task);
		this.task.addSuccessor(s1);
		this.task.addSuccessor(s2);
		s2.addSuccessor(s3);

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		log.debug(" === testReturnedBeanListNotNull");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		System.out.println("");
	}

	public void testReturnedBeanListNotEmpty() {
		log.debug(" === testReturnedBeanListNotEmpty");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertTrue("Updated bean list size > 0", updatedBeans.size() > 0);

		System.out.println("");
	}

	public void testReturnedListSize() {
		log.debug(" === testReturnedListSize");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 2", 2, updatedBeans.size());

		System.out.println("");
	}

	public void testReturnedBeanIsSameAsOriginal() {
		log.debug(" === testReturnedBeanIsSameAsOriginal");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Task given and received are equals", this.task, updatedBeans.get(0));

		System.out.println("");
	}

	public void testDatesAreOk() {
		log.debug(" === testDatesAreOk");
		log.debug("original endDate = " + this.task.getEndDate());

		float newDuration = 192f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()) + " " + ((updatedBean.getState().isASAPChainingMode()) ? "ASAP" : "Manually"));

			try {
				if (updatedBean.getName().equalsIgnoreCase("p1")) {
					fail("p1 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("t1")) {
					fail("t1 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("4.1.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("12.1.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("t2")) {
					fail("t2 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("s1")) {
					assertEquals("check task BeginDate", df.parseObject("13.1.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("16.1.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("s3")) {
					fail("s3 must not change");
				}
			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}

		System.out.println("");
	}

	public void testDurationHasNotBeenChanged() {
		log.debug(" === testDurationHasNotBeenChanged");

		Date originalBeginDate = this.task.getBeginDate();
		Date originalEndDate = this.task.getEndDate();
		float originalDuration = this.task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");
		} catch (ParseException e1) {
			fail(e1.getMessage());
		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("compute with new BeginDate = " + newBeginDate + "...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			e.printStackTrace();
			fail();
		}

		assertEquals("Assert duration value is the same", originalDuration, updatedBeans.get(0).getDuration());

		System.out.println("");
	}

	public void testRevertGivesOriginalBeginDate() {
		log.debug(" === testRevertGivesOriginalBeginDate");

		Date originalBeginDate = this.task.getBeginDate();
		Date originalEndDate = this.task.getEndDate();
		float originalDuration = this.task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		log.debug("call dateManager with new BeginDate...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, originalBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("retrieved BeginDate = " + updatedBeans.get(0).getBeginDate() + " & original BeginDate = " + originalBeginDate);
		assertEquals("Assert BeginDate reverted to original", originalBeginDate, updatedBeans.get(0).getBeginDate());

		System.out.println("");
	}

	public void testRevertGivesOriginalEndDate() {
		log.debug(" === testRevertGivesOriginalEndDate");

		float originalDuration = this.task.getDuration();
		float newDuration = 48f;

		log.debug("original duration = " + originalDuration);
		log.debug("new duration = " + newDuration);

		log.debug("call dateManager...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, originalDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());
		}

		log.debug("retrieved duration = " + updatedBeans.get(0).getDuration() + " & original duration = " + originalDuration);
		assertEquals("Assert duration reverted to original", originalDuration, updatedBeans.get(0).getDuration());

		System.out.println("");
	}

	public void testRevertGivesOriginalDuration() {
		log.debug(" === testRevertGivesOriginalDuration");

		float originalDuration = this.task.getDuration();

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("original duration = " + originalDuration);

		log.debug("call dateManager with new duration = " + newDuration + "...");
		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original duration = " + originalDuration + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_duration, originalDuration);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager .manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());
		}

		assertEquals("Assert Duration reverted to original", originalDuration, updatedBeans.get(0).getDuration());

		System.out.println("");
	}

}